var scripts = [null,
    "LingUI/js/jqGrid/jquery.jqGrid.js",
    "LingUI/js/jqGrid/i18n/grid.locale-cn.js",
    "LingUI/js/control.js",
    "LingUI/js/jsutil/js/jquery.filterSearch.js",   //筛选搜索
    "LingUI//js/ztree/js/jquery.ztree.core-3.5.js",
    null];
var _grid_id = "#demo_stu_list_table";	//表格 选择器
var _pager_id = "#demo_stu_list_pager";	//表格页脚 选择器
ace.load_ajax_scripts(scripts, function() {
    initZtree();
    //搜索回调函数
    function callback(data){
        jQuery("#demo_stu_list_table").jqGrid('setGridParam', {
            url : "../user/getSystemList.do",
            mtype : "GET",
            postData:data //传递的数据
        }).trigger("reloadGrid");
    }


    //调整以适应页面大小  其中 $(".page-content") 为jqgrid的父级容器
    $(window).off('resize.jqGrid').on('resize.jqGrid', function () {
        setTimeout(function(){
            $(_grid_id).jqGrid("setGridWidth",$(".page-content").width());
            $(".ui-jqgrid-bdiv").width($(".page-content").width()+1);
        },50);
    });
    jQuery(_grid_id).jqGrid({
        mtype : "GET",
        url: "../authorization/getServiceList.do",
        colNames:['id','服务名称','服务描述','创建时间','操作'],
        colModel:[
            {name:'id',index:'id', width:0,  editable: true,  hidden:true},
            {name:'name',index:'name', width:0,  editable: true},
            {name:'desc',index:'desc', width:0,  editable: true},
            {name:'create_time',index:'create_time', width:0,  editable: true},
            {name:'action',index:'', width:100, fixed:false, sortable:false,align:'center', formatter:
                function(cellvalue, options, rowObject) {
                    var s='<div class=" action-buttons">'
                    s=s+  '<a class="green my-tooltip-link tooltip-primary " title="查看密钥" style="cursor:pointer" ><i class="ace-icon fa fa-search-plus bigger-130 " onclick="viewrows(\''+rowObject.id+'\')"></i></a>';
                    s=s+  '<a class="blue my-tooltip-link tooltip-primary" title="编辑" style="cursor:pointer" ><i class="ace-icon fa fa-pencil bigger-130 " onclick="editrows(\''+rowObject.id+'\')"></i></a>'    ;
                    s=s+  '<a class="red my-tooltip-link tooltip-primary" title="删除" style="cursor:pointer"><i class="ace-icon fa fa-trash-o bigger-130"  onclick="deleterows(\''+rowObject.id+'\')"></i></a>' ;
                    return s+'</div>';}
            }
        ],
        pager : _pager_id, //分页信息 放置的位置
        page : lui.pageNum(_grid_id)
    });
    //自定义工具栏 =============BEGIN======================//
    jQuery(_grid_id)
        .jqGrid('navGrid',_pager_id)
        .navButtonAdd( _pager_id,{
            //iconTitle : "新增",//图标下面显示（不填的话默认为title里面的值）
            buttonicon : "ace-icon fa fa-plus-circle purple",
            onClickButton: function(){
                url="#service/editService.jsp";
                toLocation(url);
            }
        })
        .navButtonAdd( _pager_id,{
            buttonicon : "ace-icon fa fa-pencil blue"
            //onClickButton : editrows
        })
        .navButtonAdd( _pager_id,{
            buttonicon : "ace-icon fa fa-trash-o red"
            //onClickButton : deleterows
        })
        .navButtonAdd( _pager_id,{
            buttonicon : "ace-icon fa fa-refresh orange",
            onClickButton : function(){
                callback();
            }
        });
    //自定义工具栏 =============END======================//

});

//删除
function deleterows(id){
    var con = confirm("你确定要删除该记录吗？");
    if(con){
        var json = {};
        json["id"] = id;
        $.post("../authorization/deleteService.do",
            {json:JSON.stringify(json)},
            function(data){
                if(data.success == "Y"){
                    alert("删除成功!");
                    jQuery(_grid_id).trigger("reloadGrid");
                }else{
                    alert("系统异常,请联系客服人员!");
                }
            },"json");

    }else{
        return;
    }
}

function initZtree(){
    var setting = {
        view: {
            selectedMulti: false        //禁止多点选中
        },
        data: {
            simpleData: {
                enable:true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: ""
            }
        },
        callback: {
            onClick: function(treeId, treeNode) {
                var treeObj = $.fn.zTree.getZTreeObj(treeNode);
                var selectedNode = treeObj.getSelectedNodes()[0];
                //$("#txtId").val(selectedNode.id);
                //$("#txtAddress").val(selectedNode.name);
            }
        }
    };
    var zNodes =[
    ];

    $.ajax({
        url: "../authorization/getSystemList.do",
        type: "GET",
        dataType:'json',
        success: function(data){
            var node = {};
            var obj = null;
            zNodes.push(node);
            for(var i = 0;i<data.length;i++){
                obj = data[i];
                node = {};
                node["id"] = obj["id"];
                node["name"] = obj["name"];
                node["pid"] = "1";
                zNodes.push(node);
            }
            $.fn.zTree.init($("#systemTree"), setting, zNodes);
        }

    });


}
